package com.zjc.app.api.sdk.client;

import com.google.gson.reflect.TypeToken;
import com.zjc.app.api.sdk.dto.FileResponse;
import com.zjc.app.api.sdk.dto.GenericPageDTO;
import com.zjc.app.api.sdk.dto.ResultDto;
import com.zjc.app.api.sdk.dto.request.ApiDeviceLockUnlockReqDTO;
import com.zjc.app.api.sdk.dto.request.ApiDevicesLockRecordsPageReqDTO;
import com.zjc.app.api.sdk.dto.request.ApiDevicesLockRecordsReqDTO;
import com.zjc.app.api.sdk.dto.request.LockRentReqDTO;
import com.zjc.app.api.sdk.dto.result.ApiLockRecordDTO;
import com.zjc.app.api.sdk.dto.result.ApiLockRecordTuFangDTO;
import com.zjc.app.api.sdk.exception.AppBizRuntimeException;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class LockDeviceClient extends BaseClient {

    public LockDeviceClient(String serverUrl, String appId, String appSecret, Integer connectTimeout, Integer readTimeout) {
        super(serverUrl, appId, appSecret, connectTimeout, readTimeout);
    }

    /**
     * 单台设备停机
     * @param request
     * @return
     * @throws IOException
     * @throws AppBizRuntimeException
     */
    public ResultDto<String> lockDevice(ApiDeviceLockUnlockReqDTO request) throws IOException, AppBizRuntimeException {
        String url = "/v1/device/lock";
        return super.doPost(url, request, new TypeToken<ResultDto<String>>() {});
    }

    /**
     * 单台设备开机
     * @param request
     * @return
     * @throws IOException
     * @throws AppBizRuntimeException
     */
    public ResultDto<String> unlockDevice(ApiDeviceLockUnlockReqDTO request) throws IOException, AppBizRuntimeException {
        String url = "/v1/device/unlock";
        return super.doPost(url, request, new TypeToken<ResultDto<String>>() {});
    }

    /**
     * 单台设备立即/预约停开机接口
     * @param requestApiGenerateDataFileDTO
     * @return
     * @throws IOException
     * @throws AppBizRuntimeException
     */
    public ResultDto<String> lockOrUnlockDevice(ApiDeviceLockUnlockReqDTO request) throws IOException, AppBizRuntimeException {
        String url = "/v1/device/lock_or_unlock";
        return super.doPost(url, request, new TypeToken<ResultDto<String>>() {});
    }

    /**
     * 解锁车记录查询接口
     * @param request
     * @return
     * @throws IOException
     * @throws AppBizRuntimeException
     */
    public ResultDto<List<ApiLockRecordDTO>> getLockUnlockRecords(ApiDevicesLockRecordsReqDTO request) throws IOException, AppBizRuntimeException {
        String url = "/v1/device/lock_unlock/records";
        return super.doPost(url, request, new TypeToken<ResultDto<List<ApiLockRecordDTO>>>() {});
    }

    /**
     * 租赁解锁车状态查询接口
     * @param request
     * @return
     * @throws IOException
     * @throws AppBizRuntimeException
     */
    public ResultDto<String> getLockUnlockRentState(LockRentReqDTO request) throws IOException, AppBizRuntimeException {
        String url = "/v1/device/lock_unlock_rent/state";
        return super.doPost(url, request, new TypeToken<ResultDto<String>>() {});
    }
    /**
     * 解锁车记录(单台)分页查询接口 (土方)
     * @param vehicleNo
     * @param page
     * @param rows
     * @return
     * @throws IOException
     * @throws AppBizRuntimeException
     */
    public ResultDto<GenericPageDTO<ApiLockRecordTuFangDTO>> getLockUnlockRecordsPage(String vehicleNo, int page, int rows) throws IOException, AppBizRuntimeException {
        String url = "/v1/device/lock_unlock/records/page";
        Map<String, String> params = new TreeMap<>();
        params.put("vehicleNo", vehicleNo);
        params.put("page", Integer.toString(page));
        params.put("rows", Integer.toString(rows));
        return super.doGet(url, params, new TypeToken<ResultDto<GenericPageDTO<ApiLockRecordTuFangDTO>>>() {});
    }

    /**
     * 解锁车记录(列表)批量查询接口
     * @param request
     * @return
     * @throws IOException
     * @throws AppBizRuntimeException
     */
    public ResultDto<GenericPageDTO<ApiLockRecordDTO>> getDevicesLockUnlockRecords(ApiDevicesLockRecordsPageReqDTO request) throws IOException, AppBizRuntimeException {
        String url = "/v1/devices/lock_unlock/records/page";
        return super.doPost(url, request, new TypeToken<ResultDto<GenericPageDTO<ApiLockRecordDTO>>>() {});
    }

    /**
     * 解锁车记录(列表)批量查询接口 (查询每个设备的最新数据)
     * @param request
     * @return
     * @throws IOException
     * @throws AppBizRuntimeException
     */
    public ResultDto<GenericPageDTO<ApiLockRecordDTO>> getDevicesLockUnlockRecordsNewest(ApiDevicesLockRecordsPageReqDTO request) throws IOException, AppBizRuntimeException {
        String url = "/v1/devices/lock_unlock/records/newest/page";
        return super.doPost(url, request, new TypeToken<ResultDto<GenericPageDTO<ApiLockRecordDTO>>>() {});
    }

    /**
     * 成功状态的解锁车记录(列表)批量查询接口 (查询每个设备的最新数据)
     * @param request
     * @return
     * @throws IOException
     * @throws AppBizRuntimeException
     */
    public ResultDto<GenericPageDTO<ApiLockRecordDTO>> getDevicesSuccessLockUnlockRecordsNewest(ApiDevicesLockRecordsPageReqDTO request) throws IOException, AppBizRuntimeException {
        String url = "/v1/devices/lock_unlock/records/success/newest/page";
        return super.doPost(url, request, new TypeToken<ResultDto<GenericPageDTO<ApiLockRecordDTO>>>() {});
    }

    /**
     * 解锁车记录(列表)下载接口 (土方)
     * 为每个设备的最新数据
     * @param request
     * @return
     * @throws IOException
     * @throws AppBizRuntimeException
     */
    public FileResponse downloadDevicesLockUnlockRecords(ApiDevicesLockRecordsPageReqDTO request) throws IOException, AppBizRuntimeException {
        String url = "/v1/devices/lock_unlock/records.download";
        return super.downloadByPost(url, request);
    }
}
